package com.uusee.crawler.extractor;

import java.util.HashSet;
import java.util.Set;

import org.apache.oro.text.regex.MatchResult;
import org.apache.oro.text.regex.Pattern;
import org.apache.oro.text.regex.PatternCompiler;
import org.apache.oro.text.regex.PatternMatcher;
import org.apache.oro.text.regex.PatternMatcherInput;
import org.apache.oro.text.regex.Perl5Compiler;
import org.apache.oro.text.regex.Perl5Matcher;

import com.uusee.crawler.framework.Processor;
import com.uusee.crawler.model.CrawlStatusCodes;
import com.uusee.crawler.model.CrawlURI;

public class LinkExtractor extends Processor {
	private String linkReg;
	
	public LinkExtractor(String linkReg) {
		this.linkReg = linkReg;
	}
	protected void innerProcess(CrawlURI crawlUri) {
		if (!canProcess(crawlUri)) {
			return;
		}
		Set<CrawlURI> crawlUriSet = (Set<CrawlURI>)crawlUri.getModel();
		if (crawlUriSet==null) {
			crawlUriSet = new HashSet<CrawlURI>();
			crawlUri.setModel(crawlUriSet);
		}
		try {
			String sourceSite = crawlUri.getSourceSite();
			String crawlResult = crawlUri.getCrawlResult();
			
			PatternCompiler compiler = new Perl5Compiler();
			Pattern pattern = compiler.compile(linkReg, Perl5Compiler.CASE_INSENSITIVE_MASK);
			PatternMatcher matcher = new Perl5Matcher();
			PatternMatcherInput input = new PatternMatcherInput(crawlResult);
			while (matcher.contains(input, pattern)) {
				try {
					MatchResult m = matcher.getMatch();
					String link = m.group(1);
					CrawlURI crawlURI = new CrawlURI();
					crawlURI.setCrawlUrl(link);
					crawlURI.setSourceSite(sourceSite);
					
					crawlUriSet.add(crawlURI);
				} catch (Exception e) {}
			}
			crawlUri.setCrawlStatus(CrawlStatusCodes.PAGE_PROCESS_SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			//log.equals(e);
			crawlUri.setCrawlStatus(CrawlStatusCodes.PAGE_PROCESS_EXCEPTION);
		}
	}
	
	private boolean canProcess(CrawlURI crawlURI) {
		int crawlStatus = crawlURI.getCrawlStatus();
		if (crawlStatus == CrawlStatusCodes.FETCH_SUCCESS) {
			return true;
		} else if (crawlStatus == CrawlStatusCodes.FETCH_SC_NOT_OK) {
			crawlURI.setCrawlStatus(CrawlStatusCodes.PAGE_PROCESS_INVALID);
			return false;
		} else {
			return false;
		}
	}

}
